﻿using InvestmentIntelligence.DbModel.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace InvestmentIntelligence.Data.Repository.EFImplementation
{
    public class FundBookActiveSerieRepository : IIPRepository<FundBookEntityActiveSerie>, IFundBookSerieActiveRepository
    {
        public List<T> GetList<T>(Func<IQueryable<FundBookEntityActiveSerie>, IEnumerable<T>> f, int fundBookId)
        {
            return GetList(dc => f(dc.Where(x => x.FundBookEntityId == fundBookId)));
        }

        public List<FundBookEntityActiveSerie> GetList(Expression<Func<FundBookEntityActiveSerie, bool>> queryExpression)
        {
            return GetList(dc => dc.Where(queryExpression));
        }

        public FundBookEntityActiveSerie GetLastSerie(int fundBookId)
        {
            var date = GetLastDate(fundBookId);
            return Get(v => v.SingleOrDefault(x => x.FundBookEntityId == fundBookId && x.Date == date));
        }
        
        public DateTime? GetLastDate(int key)
        {
            return Get(v => v.Where(c => c.FundBookEntityId == key).Max(f => (DateTime?)f.Date));
        }
    }
}